package com.yuwj.chinaa.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.yuwj.chinaa.R;
import com.yuwj.chinaa.base.BaseActivity;
import com.yuwj.chinaa.base.ConstantParam;
import com.yuwj.chinaa.callback.BankNameSelectCallback;
import com.yuwj.chinaa.presenter.IPresenter;
import com.yuwj.chinaa.presenter.presenterImpl.PresenterImpl;
import com.yuwj.chinaa.util.SharedpreferenceUtil;
import com.yuwj.chinaa.util.ToastUtil;
import com.yuwj.chinaa.util.ValidatonCheckUtil;
import com.yuwj.chinaa.view.SelectBankNameDialog;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.URI;
import java.net.URL;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 账户实名认证界面
 */
public class AccountAuthenticationInputActivity extends BaseActivity {


    IPresenter presenter;
    SelectBankNameDialog selectBankNameDialog;
    @BindView(R.id.iv_back)
    ImageButton ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_phone_number)
    TextView tvPhoneNumber;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_idcard_number)
    EditText etIdcardNumber;
    @BindView(R.id.et_bank_name)
    EditText etBankName;
    @BindView(R.id.et_bank_number)
    EditText etBankNumber;
    @BindView(R.id.et_bank_zhih)
    EditText etBankZhih;
    @BindView(R.id.et_alipay_number)
    EditText etAlipayNumber;
    @BindView(R.id.tv_submit)
    TextView tvSubmit;

    public static Intent createIntent(Context context) {
        return new Intent(context, AccountAuthenticationInputActivity.class);
    }

    @Override
    public int setCustomContentViewResourceId() {
        return R.layout.activity_account_authentication;
    }

    @Override
    public void initParam() {
        presenter = new PresenterImpl(this);
        String[] s = new String[]{
                "中国工商银行",
                "招商银行",
                "中国农业银行",
                "中国建设银行 ",
                "中国银行",
                "中国民生银行",
                "中国光大银行",
                "中信银行",
                "交通银行",
                "兴业银行",
                "上海浦东发展银行",
                "中国人民银行",
                "华夏银行",
                "深圳发展银行",
                "广东发展银行",
                "国家开发银行",
                "中国邮政储蓄银行",
                "中国进出口银行",
                "中国农业发展银行",
                "中国银行香港分行",
                "北京银行",
                "北京农村商业银行",
                "天津银行",
                "上海银行",
                "上海农村商业银行",
                "南京银行",
                "宁波银行",
                "杭州市商业银行",
                "深圳平安银行",
                "深圳农村商业银行",
                "温州银行",
                "厦门国际银行",
                "济南市商业银行",
                "重庆银行",
                "哈尔滨银行",
                "成都市商业银行",
                "包头市商业银行",
                "南昌市商业银行",
                "贵阳商业银行",
                "兰州市商业银行",
                "常熟农村商业银行",
                "青岛市商业银行",
                "徽商银行",
                "花旗中国银行",
                "汇丰中国银行",
                "渣打中国银行",
                "香港汇丰银行",
                "渣打(香港)银行",
                "中国建设银行(亚洲)",
                "东亚银行",
                "恒生银行",
                "花旗(台湾)银行",
                "荷兰银行",
                "欧力士银行",
                "巴黎银行",
                "美国运通银行",
                "蒙特利尔银行",
                "满地可银行",
                "瑞士银行",
                "德意志银行"
        };
        selectBankNameDialog = new SelectBankNameDialog(this, s, new BankNameSelectCallback() {
            @Override
            public void onBankNameSelectSuccess(String bankName) {
                super.onBankNameSelectSuccess(bankName);
                etBankName.setText(bankName + "");
            }
        });
    }

    @Override
    public void initView() {
        tvTitle.setText("实名认证");
        tvPhoneNumber.setText(entityUser.getUserPhone());
    }

    @Override
    public void initOperator() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.tv_submit, R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                onBackPressed();
                break;
            case R.id.tv_submit:
                JSONObject param = new JSONObject();
                String phone = entityUser.getUserPhone();
                String name = etName.getText().toString();
                String shenfzh = etIdcardNumber.getText().toString();
                String bankName = etBankName.getText().toString();
                String bankZhihName = etBankZhih.getText().toString();
                String bankCardNo = etBankNumber.getText().toString();
                String alipayNo = etAlipayNumber.getText().toString();
                if (TextUtils.isEmpty(name)) {
                    ToastUtil.getInstance().shortLength(context, "姓名不能为空");
                    return;
                }
                if (TextUtils.isEmpty(shenfzh)) {
                    ToastUtil.getInstance().shortLength(context, "身份证号不能为空");
                    return;
                } else {
                    if (!ValidatonCheckUtil.isLegalIDCard(shenfzh)) {
                        ToastUtil.getInstance().shortLength(context, "身份证格式不正确");
                        return;
                    }
                }
                if (TextUtils.isEmpty(bankCardNo)) {
                    ToastUtil.getInstance().shortLength(context, "银行卡号不能为空");
                    return;
                } else {
                    if (bankCardNo.length() < 16 || bankCardNo.length() > 19) {
                        ToastUtil.getInstance().shortLength(context, "银行卡号格式不正确");
                        return;
                    }
                }
                if (TextUtils.isEmpty(bankName)) {
                    ToastUtil.getInstance().shortLength(context, "银行名称不能为空");
                    return;
                }
                if (TextUtils.isEmpty(bankZhihName)) {
                    ToastUtil.getInstance().shortLength(context, "支行名称不能为空");
                    return;
                }
                if (TextUtils.isEmpty(alipayNo)) {
                    ToastUtil.getInstance().shortLength(context, "支付宝账号不能为空");
                    return;
                }
                try {
                    param.put("phone", phone);
                    param.put("name", name);
                    param.put("identitycard", shenfzh);
                    param.put("bankname", bankName);
                    param.put("bankcard", bankCardNo);
                    param.put("bankbranch", bankZhihName);
                    param.put("alipay", alipayNo);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                presenter.verifyGeneralAuth(param.toString());
                break;
        }
    }

    @Override
    public void onVerifyGeneralAuthSuccess(String resp) {
        super.onVerifyGeneralAuthSuccess(resp);
        entityUser.setIsIdentification(ConstantParam.GENERAL_AUTH_WITH_ING);
        SharedpreferenceUtil.getInstance().saveUserInfo(context, entityUser);
        startActivity(AccountAuthenticationInreviewActivity.createIntent(context));
        finish();
    }

    @Override
    public void onVerifyGeneralAuthFail(Exception e) {
        super.onVerifyGeneralAuthFail(e);
        ToastUtil.getInstance().shortLength(context, e.getMessage() + "");
    }

}
